﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.ObjectTree;
using System.IO;

namespace ERPTemplate
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            //RuleCode.ObjectTree.Util.ExecuteSqlFile(
            //    Path.Combine(Util_DTE.AddIn_SetUpPath, "Data\\" + GlobalStaticDirectoryPath.SQLFileName)
            //    );
            //RuleCode.CurrentUtil.CompanyID = "hhh";
            //RuleCode.Table.User u = new RuleCode.Table.User();
            //u.New();
            //u.Userid.Value = u.GetRecID().ToString();
            //u.Insert();
            //this.Page.Title = Request.PhysicalApplicationPath;


            if (!IsPostBack)
            {
                RuleCode.Table.UserMenu m = 
                    RuleCode.Table.UserMenu.Find(" userid = '"+RuleCode.CurrentUtil.UserID+"'");
                InitTreeCategory(m);
            }
        }

        #region 目录树处理


        protected void InitTreeCategory(RuleCode.Table.UserMenu m)
        {
            TreeView_Category.Nodes.Clear();
            TreeNode treeNode = new TreeNode("全部", "-1");
            if (Request["MenuID"] == null || Request["MenuID"] == string.Empty)
                treeNode.Selected = true;
            TreeView_Category.Nodes.Add(treeNode);

            string filter = "";
            foreach (DataRow item in m.DT.Rows)
            {
                if (filter != "")
                {
                    filter += " and ";
                }
                filter += " PMenuID <>  '" + item["MenuID"].ToString() + "' ";
            }
            DataRow[] dRows = m.Select(filter);
            //消除重复
            Hashtable ht = new Hashtable();
            foreach (DataRow item in dRows)
            {
                if (!ht.Contains(item["PMenuID"].ToString()))
                {
                    ht.Add(item["PMenuID"].ToString(), "");
                }
            }
            foreach (string key in ht.Keys)
            {
                InitTreeNodeCollection(treeNode.ChildNodes, m, key);
            }

        }

        protected void InitTreeNodeCollection(TreeNodeCollection treeNodes, RuleCode.Table.UserMenu m, string pID)
        {
            DataRow[] dRows = m.Select("PMenuID = '" + pID + "'");
            foreach (DataRow dRow in dRows)
            {
                TreeNode childNode = new TreeNode(dRow["Label"].ToString(), dRow["MenuID"].ToString());
                if (dRow["Path"].ToString() != "")
                {
                    childNode.NavigateUrl = dRow["Path"].ToString();
                    childNode.Target = "_blank";
                }
                InitTreeNodeCollection(childNode.ChildNodes, m, dRow["MenuID"].ToString());
                if (Request["id"] != null && Request["id"] == dRow["MenuID"].ToString())
                {
                    childNode.Selected = true;
                }
                treeNodes.Add(childNode);
            }
        }

        #endregion

        protected void RuleButton1_ButtonClick(object sender, EventArgs e)
        {

        }
    }
}
